package com.itestor.iptv.modules.common.service.impl;

import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import com.itestor.iptv.modules.common.actions.RegionManageAddAction;
import com.itestor.iptv.modules.common.domain.SystemLog;
import com.itestor.iptv.modules.common.service.SystemLogService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.User;

/**
 * 
 * @author Mr.li
 *
 */
public class SystemLogInterceptor implements MethodInterceptor {

	/**
	 * Logger for this class.
	 */
	private static final Logger logger = Logger.getLogger(SystemLogInterceptor.class);

    /**
     * 
     */
    private SystemLogService systemLogService;


	/**
	 * @param invocation 参数
	 * @return object
	 * @throws Throwable any exception
	 **/
	public Object invoke(MethodInvocation invocation) throws Throwable {
		HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[2];
		
		if (invocation.getThis() instanceof RegionManageAddAction ){
		    SystemLog log = new SystemLog();
            log.setClientIp(request.getRemoteAddr());
            log.setLogDesc(RegionManageAddAction.REGIONMANAGE_ADD_LOG);
            log.setLogTime(new Date());        
            
            User user= (User)request.getSession().getAttribute(CommonConstant.SESSION_ATTR_USER);            
            log.setOperator(user.getUsername());            
            log.setLogType(CommonConstant.SYSTEM_LOG_CREATE);
            
            systemLogService.create(log);
		}
    	
		return invocation.proceed();
	}

	/**
	 * 
	 * @param pSystemLogService
	 */
    public void setSystemLogService(SystemLogService pSystemLogService) {
        systemLogService = pSystemLogService;
    }

	

}
